package cn.itcast.documentapis;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.Date;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 16:04 2019/3/7
 * @Version 1.0
 **/
public class IndexApi {

    //插入数据到es中
    public static void insert() {
        TransportClient client = esClient.getClient();
        try {
            XContentBuilder builder = jsonBuilder()
                    .startObject()
                    .field("user", "kim2chy")
                    .field("postDate", new Date())
                    .field("message", "trying out Elasticsearch")
                    .endObject();

            IndexResponse response = client.prepareIndex("twitter", "_doc", "1").setSource(builder).get();

            // Index name
            String _index = response.getIndex();
            // Type name
            String _type = response.getType();
            // Document ID (generated or not)
            String _id = response.getId();
            // Version (if it's the first time you index this document, you will get: 1)
            long _version = response.getVersion();
            RestStatus status = response.status();

            System.out.println(_index + _type + _id + _version + status);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        insert();
    }
}
